(function () {
    'use strict';

    /**
     * Config for the router
     */
    angular.module('gwBusinessApp')
        .run(['$rootScope', '$state', '$stateParams', 'Account', routerrun])
        .config(['$stateProvider', '$urlRouterProvider', routerconfig]);

    function routerrun($rootScope, $state, $stateParams, Account) {
        // function ($rootScope, $state, $stateParams) {
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
        //url监听
        $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
            if ($rootScope.user)return;
            console.log(toState);
            if (toState.name == 'access.login'
                || toState.name == 'access.register'
                || toState.name == 'access.forgotpwd') {
                return;
            }
            Account.get().$promise.then(function (account) {
                $rootScope.user = account.data;
                if (toState.name == 'access.login') {
                    $state.go("app.dashboard");//跳转到首页
                }
            }).catch(function () {
                if (toState.name != 'access.login') {
                    event.preventDefault();// 取消默认跳转行为
                    $state.go("access.login", {from: fromState.name, w: 'notLogin'});//跳转到登录界面
                    //window.location.href = 'login';
                }
            });
        });
    }

    function routerconfig($stateProvider, $urlRouterProvider) {
        $urlRouterProvider
            .otherwise('/app/dashboard');
        $stateProvider
            .state('access', {
                url: '/access',
                template: '<div ui-view class="fade-in-right-big smooth"></div>'
            })
            .state('access.login', {
                url: '/signin',
                templateUrl: 'tpl/page_signin.html',
                resolve: {
                    deps: ['uiLoad',
                        function (uiLoad) {
                            return uiLoad.load(['js/controllers/signin.js']);
                        }]
                }
            })
            .state('access.logout', {
                url: '/signup',
                templateUrl: 'tpl/page_signup.html',
                resolve: {
                    deps: ['uiLoad',
                        function (uiLoad) {
                            return uiLoad.load(['js/controllers/signup.js']);
                        }]
                }
            })
            .state('access.forgotpwd', {
                url: '/forgotpwd',
                templateUrl: 'tpl/page_forgotpwd.html'
            })
            .state('app', {
                abstract: true,
                url: '/app',
                templateUrl: 'tpl/app.html'
            })
            .state('app.dashboard', {
                url: '/dashboard',
                templateUrl: 'tpl/app_dashboard.html',
                resolve: {
                    deps: ['$ocLazyLoad',
                        function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controllers/chart.js']);
                        }]
                }
            })
            .state('access.404', {
                url: '/404',
                templateUrl: 'tpl/page_404.html'
            })
            .state('app.page', {
                url: '/page',
                template: '<div ui-view class="fade-in-down"></div>'
            })
            .state('app.page.profile', {
                url: '/profile',
                templateUrl: 'tpl/page_profile.html'
            });
    }
})();
